<?php defined('LFNC_PACKAGE') or die('forbidden');


/**
* MLT Web Engine
* http://www.molotsoft.ru
*
* @package MLT_WE
* @subpackage Lucky_Funcs
* @author Ivan Chistyakov <ivan@molotsoft.ru>
* @version $Id: mysql.lfnc.php 259 2009-06-19 05:12:52Z molotsoft@gmail.com $
* @link $HeadURL: http://lucky-funcs.googlecode.com/svn/branches/version_1/funcs/mysql.lfnc.php $
*/


function is_mysql_link($db) {
	return is_resource_type($db, 'mysql link') || is_resource_type($db, 'mysql link persistent');
}

function is_mysql_result($dbr) {
	return is_resource_type($dbr, 'mysql result');
}


function mysql_xconnect($server = 'localhost', $username = 'root', $password = '') {
	$db = mysql_connect($server, $username, $password);
	if (is_mysql_link($db)) register_shutdown_function('mysql_close', $db);
	return $db;
}


function mysql_escape($string) {
	return mysql_escape_string($string);
}

function mysql_escape_deep($value) {
	return is_array($value) ? array_map('mysql_escape_deep', $value) : mysql_escape($value);
}

function vsprintf_mysql_escape($format, $args) {
	return vsprintf($format, mysql_escape_deep($args));
}

function sprintf_mysql_escape($format /* [, $args [, $... ]] */ ) {
	$args = func_get_args();
	return vsprintf_mysql_escape($format, array_shifted($args));
}


function mysql_vqueryf($db, $query_format, $args) {
	return mysql_query(vsprintf_mysql_escape($query_format, $args), $db);
}

function mysql_queryf($db, $query_format /* [, $args [, $... ]] */ ) {
	$args = func_get_args();
	return mysql_vqueryf($db, $query_format, array_shifted($args, 2));
}


function mysql_vexecf($db, $query_format, $args) {
	return is_true($dbr = mysql_vqueryf($db, $query_format, $args)) ? mysql_affected_rows($db) : $dbr;
}

function mysql_execf($db, $query_format /* [, $args [, $... ]] */ ) {
	$args = func_get_args();
	return mysql_vexecf($db, $query_format, array_shifted($args, 2));
}


function mysql_fetch_value($dbr, $value_col = 0) {
	$row = mysql_fetch_array($dbr, is_wholenum($value_col) ? MYSQL_NUM : MYSQL_ASSOC);
	return isset($row[$value_col]) ? $row[$value_col] : FALSE;
}


function mysql_fetch_rows($dbr, $result_type = MYSQL_ASSOC) {
	if (mysql_num_rows($dbr) >= 1) mysql_data_seek($dbr, 0);
	$rows = array();
	while ($row = mysql_fetch_array($dbr, $result_type)) $rows[] = $row;
	return $rows;
}

function mysql_fetch_values($dbr, $value_col = 0) {
	if (mysql_num_rows($dbr) >= 1) mysql_data_seek($dbr, 0);
	$values = array();
	while (!is_false($value = mysql_fetch_value($dbr, $value_col))) $values[] = $value;
	return $values;
}


function mysql_fetch_rows_combine($dbr, $key_col = 'id', $result_type = MYSQL_ASSOC) {
	$rows = mysql_fetch_rows($dbr, $result_type);
	$combine = array();
	foreach (mysql_fetch_values($dbr, $key_col) as $i => $key) $combine[$key] = $rows[$i];
	return $combine;
}

function mysql_fetch_values_combine($dbr, $key_col = 'id', $value_col = 'value') {
	$values = mysql_fetch_values($dbr, $value_col);
	$combine = array();
	foreach (mysql_fetch_values($dbr, $key_col) as $i => $key) $combine[$key] = $values[$i];
	return $combine;
}


?>